package com.hitqz.robot.biz.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitqz.robot.api.business.entity.AuditorEntity;
import com.hitqz.robot.api.business.entity.RobotMapEntity;
import com.hitqz.robot.biz.service.AuditorService;
import com.hitqz.robot.biz.service.RobotMapService;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.common.log.annotation.SysLog;
import com.hitqz.robot.common.security.util.SecurityUtils;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/auditor" )
@Tag(description = "auditor" , name = "审批人员表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AuditorController {

    private final AuditorService auditorService;


    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    public R page(@ParameterObject Page page, @ParameterObject AuditorEntity query) {
        LambdaQueryWrapper<AuditorEntity> wrapper = Wrappers.lambdaQuery();
        Long id = SecurityUtils.getUser().getId();
        wrapper.eq(AuditorEntity::getCreatorId, id);
        wrapper.eq(StrUtil.isNotBlank(query.getType()), AuditorEntity::getType, query.getType());
        return R.ok(auditorService.page(page, wrapper));
    }


    @Operation(summary = "列表" , description = "列表" )
    @PostMapping("/list" )
    public R<List<AuditorEntity>> list(@RequestBody(required = false) AuditorEntity query) {
        LambdaQueryWrapper<AuditorEntity> wrapper = Wrappers.lambdaQuery();
        Long id = SecurityUtils.getUser().getId();
        wrapper.eq(AuditorEntity::getCreatorId, id);
        if (query!=null) {
            wrapper.eq(StrUtil.isNotBlank(query.getType()), AuditorEntity::getType, query.getType());
        }
        return R.ok(auditorService.list(wrapper));
    }

    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(auditorService.getById(id));
    }



    @Operation(summary = "新增审批人员" , description = "新增审批人员" )
    @PostMapping
    public R save(@RequestBody AuditorEntity add) {
        add.setCreatorId(SecurityUtils.getUser().getId());
        return R.ok(auditorService.save(add));
    }

    @Operation(summary = "修改审批人员" , description = "修改审批人员" )
    @PutMapping
    public R updateById(@RequestBody AuditorEntity update) {
        return R.ok(auditorService.updateById(update));
    }

    /**
     * 通过id删除机器人地图管理表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除审批人员" , description = "通过id删除审批人员" )
    @DeleteMapping
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(auditorService.removeBatchByIds(CollUtil.toList(ids)));
    }



}